﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using System.Threading;

namespace CloudStorageLight.Web.Tests
{
    [TestClass]
    public class CreateCloudAccountTest
    {


        [TestMethod]
        public void クラウドアカウントの作成と削除()
        {


            using (var driver = SubScenarioUtil.CreateWebDriver())
            {
                var account = "temptest";
                var email = AppUtil.GetAppConfigValue("TestEmail");
                var pass = AppUtil.GetAppConfigValue("TestPassword");
                try
                {
                    SubScenarioUtil.Navigate(driver, "Account/CreateCloudAccount");

                    IWebElement element = driver.FindElement(By.Id("CloudAccount"));
                    element.SendKeys(account);
                    element.Submit();


                    IWebElement eMailAddress = driver.FindElement(By.Id("EMailAddress"));
                    eMailAddress.SendKeys(email);
                    IWebElement password = driver.FindElement(By.Id("Password"));
                    password.SendKeys(pass);

                    IWebElement confirmPassword = driver.FindElement(By.Id("ConfirmPassword"));
                    confirmPassword.SendKeys(pass);
                    IWebElement agree = driver.FindElement(By.Id("agree"));
                    agree.Click();
                    eMailAddress.Submit();

                    SubScenarioUtil.SetUserEmailConfirmed(account, email);

                    SubScenarioUtil.Login(account, email, pass, true, driver);

                    SubScenarioUtil.Navigate(driver, "Settings/CloudSettings", account);

                    SubScenarioUtil.Navigate(driver, "Settings/Configuration", account);
                    var blobTypeItem = driver.FindElement(By.Id("BlobTypeItem"));
                    blobTypeItem.FindElement(By.XPath("./option[1]")).Click();
                    driver.FindElement(By.Id("blobAddBtn")).Click();

                    var blobPath = SubScenarioUtil.TryFind(driver, By.Id("Path"));
                    blobPath.SendKeys("TestBlob");

                    var connectionString = driver.FindElement(By.Id("ConnectionString"));
                    connectionString.SendKeys(AppUtil.GetAppConfigValue("TestBlobConnectionString"));

                    var blobContainerName = driver.FindElement(By.Id("BlobContainerName"));
                    blobContainerName.SendKeys("unittest");
                    var entry = SubScenarioUtil.TryFind(driver, By.Name("submitButton"));
                    entry.Click();

                    var folderList = driver.FindElements(By.ClassName("folderpath"));
                    Assert.AreEqual(1, folderList.Count);

                    SubScenarioUtil.Navigate(driver, "f/TestBlob/", account);


                    SubScenarioUtil.Navigate(driver, "Settings/Configuration", account);
                    blobTypeItem = driver.FindElement(By.Id("BlobTypeItem"));
                    blobTypeItem.FindElement(By.XPath("./option[3]")).Click();
                    driver.FindElement(By.Id("blobAddBtn")).Click();

                    var blobPath2 = SubScenarioUtil.TryFind(driver, By.Id("Path"));
                    blobPath2.SendKeys("TestFtp");

                    var ftpUrl = driver.FindElement(By.Id("FtpUrl"));
                    ftpUrl.SendKeys(AppUtil.GetAppConfigValue("FtpUrl"));

                    var ftpFolderPath = driver.FindElement(By.Id("FtpFolderPath"));
                    ftpFolderPath.SendKeys("/");

                    var user = driver.FindElement(By.Id("User"));
                    user.SendKeys(AppUtil.GetAppConfigValue("FtpUser"));

                    password = driver.FindElement(By.Id("Password"));
                    password.SendKeys(AppUtil.GetAppConfigValue("FtpPassword"));

                    entry = SubScenarioUtil.TryFind(driver, By.Name("submitButton"));
                    entry.Click();


                    var folderList2 = driver.FindElements(By.ClassName("folderpath"));
                    Assert.AreEqual(2, folderList2.Count);

                    SubScenarioUtil.Navigate(driver, "f/FtpBlob/", account);

                    SubScenarioUtil.Navigate(driver, "f/", account);
                    SubScenarioUtil.ClickMenuCommand(driver, "showUserListCommand");

                    SubScenarioUtil.TryFind(driver, By.Id("adduser-command")).Click();

                    SubScenarioUtil.TryFind(driver, By.Id("username")).SendKeys("user1@sample.com");
                    SubScenarioUtil.TryFind(driver, By.Id("userpassword")).SendKeys("User1@sample.com");
                    SubScenarioUtil.TryFind(driver, By.Id("confirmuserpassword")).SendKeys("User1@sample.com");
                    SubScenarioUtil.TryFind(driver, By.Id("adduser")).Click();

                    IWebElement role = null;
                    for (int i = 0; i < 100; i++)
                    {
                        try
                        {
                            role = driver.FindElement(By.Id("user1@sample.com_Delete"));
                            if (role != null) break;
                        }
                        catch (Exception)
                        {
                            Thread.Sleep(1000);
                        }
                        if (i == 99) throw new NotFoundException("user1@sample.com_Delete");
                    }
                    driver.FindElement(By.LinkText("所属グループ")).Click();
                    var newRole = driver.FindElement(By.Id("newRole"));
                    newRole.SendKeys("営業");
                    driver.FindElement(By.Id("addNewRole")).Click();
                    driver.FindElement(By.Id("rolesave")).Click();

                    driver.FindElement(By.Id("user1@sample.com_Save")).Click();
                    SubScenarioUtil.WaitBusy(driver);

                    IWebElement reset = null;
                    for (int i = 0; i < 100; i++)
                    {
                        try
                        {
                            reset = driver.FindElement(By.Id("user1@sample.com_RsetPassword"));
                            if (reset != null) break;
                        }
                        catch (Exception)
                        {
                            Thread.Sleep(1000);
                        }
                        if (i == 99) throw new NotFoundException("user1@sample.com_RsetPassword");
                    }
                    reset.Click();

                    var resetpass = SubScenarioUtil.TryFind(driver, By.Id("resetUserpassword"));
                    var resetpassconf = driver.FindElement(By.Id("resetConfirmuserpassword"));
                    resetpass.SendKeys(pass);
                    resetpassconf.SendKeys(pass);
                    resetpassconf.Submit();

                    SubScenarioUtil.Navigate(driver, "Account/UserList", account);


                    IWebElement del = null;
                    for (int i = 0; i < 100; i++)
                    {
                        try
                        {
                            del = driver.FindElement(By.Id("user1@sample.com_Delete"));
                            if (del != null) break;
                        }
                        catch (Exception)
                        {
                            Thread.Sleep(1000);
                        }
                        if (i == 99) throw new NotFoundException("user1@sample.com_Delete");
                    }
                    del.Click();
                    driver.SwitchTo().Alert().Accept();
                    Assert.IsFalse(SubScenarioUtil.Exists(driver, "user1@sample.com"));




                    SubScenarioUtil.Navigate(driver, "Account/ForgotPassword", account);
                    IWebElement email2 = driver.FindElement(By.Id("Email"));
                    email2.Submit();
                    email2 = driver.FindElement(By.Id("Email"));
                    email2.SendKeys(email);
                    email2.Submit();

                    var resetUrl = "Account/ResetPassword?userId=xxxx&code=xxxx";
                    SubScenarioUtil.Navigate(driver, resetUrl, account);
                    IWebElement email2b = driver.FindElement(By.Id("Email"));
                    email2b.Submit();
                    email2b = driver.FindElement(By.Id("Email"));
                    email2b.SendKeys(email);
                    IWebElement password2 = driver.FindElement(By.Id("Password"));
                    password2.SendKeys(pass);
                    IWebElement confirmPassword2 = driver.FindElement(By.Id("ConfirmPassword"));
                    confirmPassword2.SendKeys(pass);
                    email2b.Submit();

                    SubScenarioUtil.Navigate(driver, "Account/Register", account);
                    IWebElement email3 = driver.FindElement(By.Id("Email"));
                    email3.Submit();
                    email3 = driver.FindElement(By.Id("Email"));
                    email3.SendKeys(email);
                    IWebElement password3 = driver.FindElement(By.Id("Password"));
                    password3.SendKeys(pass);
                    IWebElement confirmPassword3 = driver.FindElement(By.Id("ConfirmPassword"));
                    confirmPassword3.SendKeys(pass);
                    email3.Submit();


                    SubScenarioUtil.Navigate(driver, "Settings/DeleteCloudAccount", account);
                    IWebElement agree2 = driver.FindElement(By.Id("agree"));
                    agree2.Click();
                    agree2.Submit();

                    var deletedAccount = driver.FindElement(By.Id("CloudAccount"));
                    Assert.AreEqual(account, deletedAccount.GetAttribute("Value"));
                }
                catch (Exception)
                {
                    try
                    {
                        SubScenarioUtil.SetUserEmailConfirmed(account, email);
                        SubScenarioUtil.Login(account, email, pass, true, driver);
                        SubScenarioUtil.Navigate(driver, "Settings/DeleteCloudAccount", account);
                        IWebElement agree2 = driver.FindElement(By.Id("agree"));
                        agree2.Click();
                        agree2.Submit();
                    }
                    catch (Exception)
                    {
                    }

                    throw;
                }
            }

        }



        [TestMethod]
        public void SharePointクラウドアカウントの作成と削除()
        {
            using (var driver = SharePointSubScenarioUtil.CreateWebDriver())
            {
                var account = "proaxiapublic";
                var newsite = AppUtil.GetAppConfigValue("SharePointNewSiteUrl");
                var email = AppUtil.GetAppConfigValue("SharePointUser");
                var pass = AppUtil.GetAppConfigValue("SharePointPassword");
                try
                {
                    SharePointSubScenarioUtil.Navigate(driver, "Account/CreateCloudAccount");

                    IWebElement element = driver.FindElement(By.Id("CloudAccount"));
                    element.SendKeys(account);

                    IWebElement site = driver.FindElement(By.Id("SpHostUrl"));
                    site.SendKeys(newsite);

                    IWebElement eMailAddress = driver.FindElement(By.Id("EMailAddress"));
                    eMailAddress.SendKeys(email);
                    IWebElement password = driver.FindElement(By.Id("Password"));
                    password.SendKeys(pass);

                    IWebElement agree = driver.FindElement(By.Id("agree"));
                    agree.Click();
                    eMailAddress.Submit();

                    SharePointSubScenarioUtil.Navigate(driver, "Settings/CloudSettings", account);

                    SharePointSubScenarioUtil.Navigate(driver, "Settings/Configuration", account);
                    var blobTypeItem = driver.FindElement(By.Id("BlobTypeItem"));
                    blobTypeItem.FindElement(By.XPath("./option[1]")).Click();
                    driver.FindElement(By.Id("blobAddBtn")).Click();

                    var blobPath = SubScenarioUtil.TryFind(driver, By.Id("Path"));
                    blobPath.SendKeys("TestBlob");

                    var connectionString = driver.FindElement(By.Id("ConnectionString"));
                    connectionString.SendKeys(AppUtil.GetAppConfigValue("TestBlobConnectionString"));

                    var blobContainerName = driver.FindElement(By.Id("BlobContainerName"));
                    blobContainerName.SendKeys("unittest");
                    var entry = SharePointSubScenarioUtil.TryFind(driver, By.Name("submitButton"));
                    entry.Click();

                    var folderList = driver.FindElements(By.ClassName("folderpath"));
                    Assert.AreEqual(1, folderList.Count);

                    SharePointSubScenarioUtil.Navigate(driver, "f/TestBlob/", account);


                    SharePointSubScenarioUtil.Navigate(driver, "Settings/Configuration", account);
                    blobTypeItem = driver.FindElement(By.Id("BlobTypeItem"));
                    blobTypeItem.FindElement(By.XPath("./option[3]")).Click();
                    driver.FindElement(By.Id("blobAddBtn")).Click();

                    var blobPath2 = SharePointSubScenarioUtil.TryFind(driver, By.Id("Path"));
                    blobPath2.SendKeys("TestFtp");

                    var ftpUrl = driver.FindElement(By.Id("FtpUrl"));
                    ftpUrl.SendKeys(AppUtil.GetAppConfigValue("FtpUrl"));

                    var ftpFolderPath = driver.FindElement(By.Id("FtpFolderPath"));
                    ftpFolderPath.SendKeys("/");

                    var user = driver.FindElement(By.Id("User"));
                    user.SendKeys(AppUtil.GetAppConfigValue("FtpUser"));

                    password = driver.FindElement(By.Id("Password"));
                    password.SendKeys(AppUtil.GetAppConfigValue("FtpPassword"));

                    entry = SharePointSubScenarioUtil.TryFind(driver, By.Name("submitButton"));
                    entry.Click();


                    var folderList2 = driver.FindElements(By.ClassName("folderpath"));
                    Assert.AreEqual(2, folderList2.Count);

                    SharePointSubScenarioUtil.Navigate(driver, "f/FtpBlob/", account);

                    SharePointSubScenarioUtil.Navigate(driver, "Settings/DeleteCloudAccount", account);
                    IWebElement agree2 = driver.FindElement(By.Id("agree"));
                    agree2.Click();
                    agree2.Submit();


                }
                catch (Exception)
                {
                    try
                    {
                        SharePointSubScenarioUtil.Navigate(driver, "Account/Login");
                        IWebElement element = driver.FindElement(By.Id("cloudAccount"));
                        element.SendKeys(account);
                        element = driver.FindElement(By.Id("email"));
                        element.SendKeys(email);
                        element = driver.FindElement(By.Id("password"));
                        element.SendKeys(pass);
                        element.Submit();

                        SharePointSubScenarioUtil.Navigate(driver, "Settings/DeleteCloudAccount", account);
                        IWebElement agree2 = driver.FindElement(By.Id("agree"));
                        agree2.Click();
                        agree2.Submit();

                    }
                    catch (Exception)
                    {
                    }
                    throw;
                }
            }

        }


    }
}
